import json
import time

from channels.generic.websocket import WebsocketConsumer
from channels.generic.websocket import AsyncWebsocketConsumer
import asyncio
class MonitorConsumer(AsyncWebsocketConsumer):
# class MonitorConsumer(WebsocketConsumer):
    async def connect(self):
        print('客户端连接了')
        await self.accept()
        while True:
            await asyncio.sleep(1)
            await self.send(text_data=json.dumps({"message": 'hello'}))

    async def disconnect(self, close_code):
        print('客户端断开了')
        await self.close(666)
        pass

    async def receive(self, text_data):
        print('客户端发数据了')
        # text_data_json = json.loads(text_data)
        # message = text_data_json["message"]

        await self.send(text_data=json.dumps({"message": 'hello'}))
